package cheme.ProcessModel;
import java.util.HashMap;
import java.util.Iterator;

//import org.eclipse.ui.console.MessageConsole;

import cheme.ProcessModel.Interpreter.Interpreter;
import cheme.ProcessModel.Interpreter.ModelElementData;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.AbstractTagComponent;

public class ConsoleModel
{
	private static HashMap<IModelElement,String> consoleMap = new HashMap<IModelElement,String>();
	//public static MessageConsole messageConsole = null;		
	private static boolean bFlagDirty = false; // True - valid change made to the map. Write the changed log in file.
	private static int messageCount = 0;
	private IModelElement modelElement;
	private ConsoleView cvm;
	//	 Create Log File
	//static final Logger log = new Logger();
	
	public ConsoleModel()
	{	
	
	}
	
	public int createMap(IModelElement elementkey, String messageValue)
	{
		//IModelElement elementkey = ruleResult.getSource();
		//String messageValue = ruleResult.getMessage();
		messageValue = messageValue.replaceAll("\n", " ");
		
		// Polpulate the console hash map
		if(null != elementkey)
		{
			
				//if mapping is not already present
				if(!consoleMap.containsKey(elementkey))
				{
					// add the key-value to the map
					messageCount++;
					messageValue = Integer.toString(messageCount) + " " + messageValue;
					consoleMap.put(elementkey, messageValue);					
				}	
				// if element key is present, it means that only message has changed with the model element
				// create new entry for that element
				else
				{
					consoleMap.remove(elementkey);
					messageCount++;
					messageValue = Integer.toString(messageCount) + " " + messageValue;
					consoleMap.put(elementkey, messageValue);			
				}
				// Add the entry in Log
				// <Added,Location, Message>
				String elementString = elementkey.toString().trim()+ ", ";
        		ProcessModelConstants.log.Write("Added, ",elementString, messageValue);
				return messageCount;
		}		
		
		return 0;
	}
	
	// Remove the mapping from the map
	public static boolean removeEntry(IModelElement elementkey)
	{
		boolean bEntryremoved = false;
		//IModelElement elementkey = ruleResult.getSource();
		if(null != elementkey && consoleMap.containsKey(elementkey))
		{
			String messageValue = consoleMap.get(elementkey);
			consoleMap.remove(elementkey);
			bEntryremoved = true;
			// Add the entry in Log
			//<Removed,Location, Message>
			String elementString = elementkey.toString().trim();
			elementString = elementString + ", ";
    		ProcessModelConstants.log.Write("Removed, ",elementString, messageValue);
		}
		return bEntryremoved;
	}
	
	// Display the messages in the console
	public void displayMessage()
	{
		/*if(bFlagDirty)
		{
			ProcessModelConstants.log.printSpace();
		}*/
		
		Iterator It = consoleMap.keySet().iterator();
        while (It.hasNext())
        {
        	IModelElement elementkey = (IModelElement)(It.next());  	
            
        	String message = consoleMap.get(elementkey);
        	cvm.setMessage(message);
        	
        	/*if(bFlagDirty){        		
        		String elementString = elementkey.toString().trim();
        		ProcessModelConstants.log.Write(elementString,message);      		
        		
        		}*/
         }
        
        //bFlagDirty = false;        
	}
		
	
	public void resetConsole()
	{
		if(null != cvm)
		{
			cvm.clearAll();	
		}
		
	}
	
	// This methos checks if element is already present in the map
	public boolean containsElement(IModelElement element)
	{
		if(null != element && consoleMap.containsKey(element))
		{
			return true;
		}
		return false;
	}
	
	// This method returs the message associated with the element in the map
	public boolean containsMessage(IModelElement element, String message)
	{
		if(null != element && consoleMap.containsKey(element))
		{
			String msgtoChk = (consoleMap.get(element)).substring(2);
			msgtoChk.trim();
			message = message.replaceAll("\n", " ");
			message.trim();
			// I did this becuase I was not able to get rid of leading white spaces in msgtochk.
			// Leading white spaces come in msgtochk when msgcount goes beyond 9, i.e in double figures.
			String newmessage = " " + message;		
			
			if(msgtoChk.equals(message) || msgtoChk.equals(newmessage))
			{
				return true;
			}
		}
		return false;
	}
	
	// Returns the message index from the message
	//"smiles".substring(1, 5) returns "mile"
	public String getMessageIndex(IModelElement element)
	{
		if(null != element && consoleMap.containsKey(element))
		{
			String msgIndex = (consoleMap.get(element));
			msgIndex = msgIndex.substring(0, 2);		
			msgIndex.trim();
			
			return msgIndex;
		}
		return "";
	}

	public void setModelElement(IModelElement object) 
	{
		if(object instanceof ProcessModel)
		{
			ProcessModel pm = (ProcessModel)object;
			Iterator<IChild> itr = pm.iterator();
			itr.next();// skip first element
			cvm = (ConsoleView)itr.next();
		}
			
	}
		
	// returns the populated hash map.
	public static HashMap<IModelElement,String> getMap()
	{
		if(consoleMap.isEmpty())
		{
			return null;
		}
		return consoleMap;
	}
	
	public void setFlagDirty()
	{
		bFlagDirty = true;
	}
	
	// When a model element is removed from the system by deleting a strem or pu
	// this methd is used to remove the corresponding feedback messge from the console view
	public static void checkModel(IModelElement element)
	{
		if(consoleMap.isEmpty())
		{
			return;
		}
		//consoleMap.remove(element);	
		removeEntry(element);
	}


}
